//--------------------------------------------------------------------------------
// This file is a portion of the D2D1Renderer.  It is distributed
// under the MIT License, available in the root of this distribution and 
// at the following URL:
//
// http://www.opensource.org/licenses/mit-license.php
//
// Copyright (c) Mirco Timmermann
//
//--------------------------------------------------------------------------------

/* EXAMPLE:
	TreeListNode* root = new TreeListNode("root");
	root->open = true;

	TreeListNode* A = new TreeListNode("A");

	TreeListNode* B = new TreeListNode("B");
		TreeListNode* B_A = new TreeListNode("B_A");
		TreeListNode* B_B = new TreeListNode("B_B");

	TreeListNode* C = new TreeListNode("C");

	root->addChild(A);
	root->addChild(B);
	root->addChild(C);

	B->addChild(B_A);
	B->addChild(B_B);

	//
	TreeListNode* root2 = new TreeListNode("root2");
	root->open = true;

	TreeListNode* A2 = new TreeListNode("A2");
	root2->addChild(A2);
	
	//
	_treeList->addChild(root);
	_treeList->addChild(root2);
*/
#pragma once

#include "..\DisplayObject.h"


namespace D2 {

class TreeListNode {
public:
	std::string _name;
	TreeListNode* _parent;
	std::vector<TreeListNode*> _childs;

	bool open;
	bool draggable;

	TreeListNode() {
		_name = "";
		_parent = 0;

		open = false;
		draggable = false;
	}

	TreeListNode(std::string name) {
		_name = name;

		open = false;
		draggable = false;
	}

	void addChild(TreeListNode* node) {
		_childs.push_back(node);
        node->_parent = this;
    }

	bool hasChilds() {
		return (_childs.size() != 0);
	}
};

class TreeList : public TreeListNode, public DisplayObject {
private:
	struct NodeIterateInfo {
		int index;
		TreeListNode* node;

		NodeIterateInfo() {
			index = 0;
			node = 0;
		}
	};

public:
	TreeList(int x, int y, int width, int height);
	~TreeList(void);

	virtual bool Draw(Direct2DContext* ctx, D2D1::Matrix3x2F* mtx = 0);

private:
	bool showDoubleSelected;
	bool showSelected;
    
    //int _flashSelectCounter;
    
    int _currentSelectIndex;
    int _currentDoubleClickIndex;
    int _currentMouseOverIndex;
    int _currentDragIndex;
    int _currentMouseOverSector;
    
    
    TreeListNode* _mouseSelectElement;
	TreeListNode* _mouseDoubleClickElement;
	TreeListNode* _mouseOverElement;
	TreeListNode* _mouseDragElement;
    
    int _elementHeight;
    int _totalElementHeight;
    
    //_elements = new Array();
    int _offsetIndex;
    
    bool permanentUpdate;
    
    bool enableInnerDrag;
    
//////////////SAME AS LIST.JS
    
    //TODDO:gucken ob gebaurcht wird
    int numElements;

public:
	/*
	this.onMouseDown = this.ON_MOUSE_DOWN;
        this.onMouseOut = this.ON_MOUSE_OUT;
        this.onMouseDoubleClick = this.ON_MOUSE_DOUBLE_CLICK;
        this.onMouseMove = this.ON_MOUSE_MOVE;
        this.onMouseDrag = this.ON_MOUSE_DRAG;
        this.onMouseLeave = this.ON_MOUSE_LEAVE;
        this.onMouseUp = this.ON_MOUSE_UP;
	*/

	virtual void OnMouseDown0(const MouseEvent &e);
	virtual void OnMouseOut(const MouseEvent &e);
	//doppel click
	virtual void OnMouseMove(const MouseEvent &e);
	virtual void OnMouseDrag0(const MouseEvent &e);
	virtual void OnMouseLeave0(const MouseEvent &e);
	virtual void OnMouseUp0(const MouseEvent &e);
	virtual void OnMouseDoubleClick0(const MouseEvent &e);

	virtual void OnAdded(const Event &e);

private:
	int ParseObjectTree_draw(TreeListNode* node, Direct2DContext* ctx, int elementCount, int level);

	int GetIndex(float y);
	TreeListNode* GetNode(int index);

	NodeIterateInfo ParseObjectTree_getNode(TreeListNode* node, int index, NodeIterateInfo info = NodeIterateInfo());

private:
	void UpdateHeight();

};

}